package com.example.demo.buessiness.department.controller;


import com.example.demo.buessiness.department.interfaces.IDepartmentService;
import com.example.demo.buessiness.department.model.dto.DepartmentRequest;
import com.example.demo.buessiness.department.model.dto.DepartmentResponse;
import com.example.demo.buessiness.user.model.dto.UserRequest;
import com.example.demo.buessiness.user.model.dto.UserResponse;
import msa.common.enums.CodeEnum;
import msa.common.model.DataTableVO;
import msa.common.model.PageModel;
import msa.common.model.ResultCode;
import msa.common.model.ResultModel;
import msa.common.utils.ResultModelUtils;
import org.hibernate.annotations.ResultCheckStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/fb")
@CrossOrigin(allowCredentials = "true", allowedHeaders = {"Authentication-Info", "content-type"},
        exposedHeaders = {"Authentication-Info"})
public class DepartmentController {

    @Autowired
    IDepartmentService defClientService;


    /**
     * 新增客户端
     * @param request
     * @return
     */
    @PostMapping(value = "/addDepartment",produces = "application/json;charset=UTF-8")
    public ResultModel addUser(@RequestBody DepartmentRequest request){
        if (request == null) {
            return ResultModelUtils.getFailInstance(CodeEnum.PARAM_ERROR,"数据为null");
        }
        try {
            return defClientService.addDepartment (request);
        } catch (Exception e) {
            return ResultModelUtils.getFailInstance();
        }
    }

    /**
     * 修改客户端
     * @param data
     * @return
     */

    @PutMapping(value = "updateDepartment",produces = "application/json;charset=UTF-8")
    public ResultModel updateUser(@RequestBody DepartmentRequest data){
        if (data == null) {
            return ResultModelUtils.getFailInstance(CodeEnum.PARAM_ERROR,"数据为null");
        }

        try {
            return defClientService.updateDepartment (data);
        } catch (Exception e) {
            return ResultModelUtils.getFailInstance();
        }
    }


    /**
     * 删除客户端
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delDepartment")
    public ResultModel delUser(@RequestParam("id") Long id){
        try {
            return defClientService.deleteDepartment (id);
        } catch (Exception e) {
            return ResultModelUtils.getFailInstance();
        }
    }


    /**
     * 获取所有客户端
     * @return
     */
    @GetMapping(value = "/departmentAll")
    public ResultModel findAllDepartment(){
        try {
            return ResultModelUtils.getSuccessInstance (defClientService.findAll ());
        } catch (Exception e) {
            return ResultModelUtils.getFailInstance (ResultCode.BUSINESS_ERROR.toString (),e.getMessage ());
        }
    }

    /**
     * 获取单个客户端详情
     * @param id
     * @return
     */

    @GetMapping(value = "/department/findById/{id}")
    public ResultModel findDepartmentById(@PathVariable("id") Long id){
        try{
            return defClientService.findDepartmentByID(id);
        }catch (Exception e){
            return ResultModelUtils.getFailInstance(ResultCode.BUSINESS_ERROR.toString(),e.getMessage());
        }
    }
    /**
     * 获取单个客户端详情
     * @param departmentRequest
     * @return
     */

    @PostMapping(value = "/findByDepartment")
    public ResultModel findDepartmentByName(@RequestBody DepartmentRequest departmentRequest){
        try {
            List<DepartmentResponse> departmentResponses = defClientService.findByDepartmentname(departmentRequest.getDepartment());
            if (departmentResponses.isEmpty()) {
                return ResultModelUtils.getInstance(false);
            } else {
                return ResultModelUtils.getSuccessInstance(departmentResponses);
            }
        } catch (Exception e) {
            return ResultModelUtils.getFailInstance(ResultCode.BUSINESS_ERROR.toString(), e.getMessage());
        }
    }

    /**
     * 客户端分页查询
     * @param start 起始位置
     * @param length 每页显示记录数
     * @param department 部门名称
     * @param company 公司
     * @return
     */
    @GetMapping(value = "/page")
    public DataTableVO page(@RequestParam Integer start,
                            @RequestParam Integer length,
                            @RequestParam(value = "param",required = false) String department,
                            @RequestParam(value = "chName",required = false) String company){
        DepartmentRequest request = new DepartmentRequest ();
        request.setDepartment (department);
        request.setCompany (company);
        PageModel pageModel = PageModel.builder()
                .start(start)
                .pageSize(length)
                .searchData(request)
                .build();
        return defClientService.page (pageModel);

    }
}
